class ProfileManager {
    constructor() {
        this.profileContainer = document.querySelector('.profile-container');
        this.progressBar = document.querySelector('.progress');
        this.statNumber = document.querySelector('.stat-number');
        
        this.loadUserProfile();
    }

    async loadUserProfile() {
        try {
            const profile = await this.fetchUserProfile();
            this.updateProfile(profile);
        } catch (error) {
            console.error('加载用户资料失败:', error);
        }
    }

    updateProfile(profile) {
        // 更新头像
        const avatar = this.profileContainer.querySelector('.avatar img');
        avatar.src = profile.avatar || 'images/default-avatar.png';

        // 更新用户信息
        const username = this.profileContainer.querySelector('.profile-info h2');
        username.textContent = profile.username;

        const joinDate = this.profileContainer.querySelector('.profile-info p');
        joinDate.textContent = `加入时间：${new Date(profile.joinDate).toLocaleDateString()}`;

        // 更新数据上传进度
        this.progressBar.style.width = `${profile.uploadProgress}%`;

        // 更新记录天数
        this.statNumber.textContent = profile.recordDays;

        // 添加数据统计卡片
        this.updateStatistics(profile.statistics);
    }

    updateStatistics(statistics) {
        const statsContainer = document.createElement('div');
        statsContainer.className = 'statistics-container';
        
        const statsHTML = `
            <div class="stat-card">
                <h3>数据分析</h3>
                <div class="stat-grid">
                    <div class="stat-item">
                        <span class="stat-label">总字数</span>
                        <span class="stat-value">${statistics.totalWords}</span>
                    </div>
                    <div class="stat-item">
                        <span class="stat-label">关键词数</span>
                        <span class="stat-value">${statistics.keywordCount}</span>
                    </div>
                    <div class="stat-item">
                        <span class="stat-label">情感得分</span>
                        <span class="stat-value">${statistics.sentimentScore}</span>
                    </div>
                    <div class="stat-item">
                        <span class="stat-label">活跃度</span>
                        <span class="stat-value">${statistics.activityScore}</span>
                    </div>
                </div>
            </div>
            <div class="stat-card">
                <h3>最近活动</h3>
                <div class="activity-list">
                    ${statistics.recentActivities.map(activity => `
                        <div class="activity-item">
                            <i class="fas ${this.getActivityIcon(activity.type)}"></i>
                            <div class="activity-content">
                                <div class="activity-text">${activity.text}</div>
                                <div class="activity-time">${new Date(activity.time).toLocaleString()}</div>
                            </div>
                        </div>
                    `).join('')}
                </div>
            </div>
        `;

        statsContainer.innerHTML = statsHTML;
        this.profileContainer.appendChild(statsContainer);
    }

    getActivityIcon(type) {
        const icons = {
            upload: 'fa-upload',
            chat: 'fa-comments',
            login: 'fa-sign-in-alt',
            update: 'fa-pen',
            analyze: 'fa-chart-bar'
        };
        return icons[type] || 'fa-circle';
    }

    // API 调用方法
    async fetchUserProfile() {
        // TODO: 实现从服务器获取用户资料的逻辑
        return {
            username: '测试用户',
            avatar: null,
            joinDate: '2024-01-03',
            uploadProgress: 45,
            recordDays: 7,
            statistics: {
                totalWords: 12345,
                keywordCount: 89,
                sentimentScore: 8.5,
                activityScore: 92,
                recentActivities: [
                    {
                        type: 'upload',
                        text: '上传了新的日记内容',
                        time: new Date(Date.now() - 3600000)
                    },
                    {
                        type: 'chat',
                        text: '与数字生命进行了对话',
                        time: new Date(Date.now() - 7200000)
                    },
                    {
                        type: 'analyze',
                        text: '生成了新的思维云图',
                        time: new Date(Date.now() - 86400000)
                    }
                ]
            }
        };
    }
}

// 创建个人中心管理器实例
window.profileManager = new ProfileManager(); 